package com.hefeng.unit.common;



import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.hefeng.common.core.utils.DateUtils;
import com.hefeng.common.redis.service.RedisService;
import com.hefeng.unit.domain.GyDeviceReport;
import com.hefeng.unit.domain.entity.UserInfo;
import com.hefeng.unit.util.Sample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.Executor;

/**
 * 方法说明: 多线程短信发送类
 * @Author: huzh
 * @Date: 2022/11/3
 */
@Component
public class MsgTaskSend {
    @Autowired
    RedisService redisService;
    @Autowired
    @Qualifier("taskExecutor")  //指定某个bean
    private Executor taskExecutor;
    //启动多线程发送短信
    public  void sendMsg(){
        taskExecutor.execute(new Runnable() {
            @Override
            public void run() {
                List<GyDeviceReport> list=StartLoadingParameter.DataList;
                try {
                    list.forEach(obj->{
                        List<UserInfo>userInfos=redisService.getCacheList(StartLoadingParameter.BINDING);
                        userInfos.forEach(user->{
                            if(obj.getDeviceCode().equals(user.getDeviceCode())) {
                                //发送短信
                                com.aliyun.dysmsapi20170525.Client client = null;
                                try {
                                    client = Sample.createClient("LTAI4G9NUVeKwrPQh9QwPyMh", "P1NuKkn65iCM9YjunAfhCpBIN062Xg");

                                    SendSmsRequest sendSmsRequest = new SendSmsRequest()
                                            .setPhoneNumbers("18356515503")
                                            .setSignName("海南合丰运维科技有限公司")
                                            .setTemplateCode("SMS_235814724")
                                            .setTemplateParam("{\"time\":\"" + DateUtils.format(obj.getReportTime(), DateUtils.YYYY_MM_DD_HH_MM_SS) + "\",\"event\":\"" + obj.getReportContent() + "\"}");
                                    // 复制代码运行请自行打印 API 的返回值
//                                    SendSmsResponse sendResp = client.sendSms(sendSmsRequest);
//                                    String code = sendResp.body.code;
//                                    if (!com.aliyun.teautil.Common.equalString(code, "OK")) {
//                                        com.aliyun.teaconsole.Client.log("错误信息: " + sendResp.body.message + "");
//                                    }

                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }
                        });
                        obj.setIsSmsNotice(1);
                    });
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        //主线程存储数据库

    }
}